package hibernate.xml43.view;

import hibernate.xml43.pojo.Dept;
import hibernate.xml43.util.HibernateHandler;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class View2 {
	
	public static void main(String[] args) {
		//查询
		addDept();
	}
	//删除
	public static void deleteDept() {
		Session session = HibernateHandler.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Dept d = (Dept)session.load(Dept.class, 1);
		session.delete(d);
		transaction.commit();
		session.close();
	}
	//插入
	public static void addDept() {
		//通过自己的类获取SessionFactory使其为单例(一个数据库对应一个SessionFactory就行了)
		Session session = HibernateHandler.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		//此句相当于insert语句
		Dept d = new Dept();
		d.setDname("bbb");
		d.setLoc("b区");
		session.save(d);
		transaction.commit();
		session.close();
	}
	//修改
	public static void updateDept() {
		Session session = HibernateHandler.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		//此句相当于select语句
		Dept d = (Dept) session.load(Dept.class, 1);
		//此句相当于update语句
		//2句合起来就是更新主键为1的记录的dname修改为xxx
		d.setDname("xxx");
		transaction.commit();
		session.close();
	}

}
